package org.acb.sgp.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.acb.sgp.beans.Actor;
import org.acb.sgp.dao.ActorDao;
import org.acb.sgp.factory.ConnectionFactory;

public class ActorDaoImpl implements ActorDao {

    private Connection con;
    private CallableStatement cs;
    private ResultSet rs;

    private Connection getConnection() throws SQLException {
        return ConnectionFactory.getInstance().getConnection();
    }

    @Override
    public void registrarActor(Actor actor) {

    }

    @Override
    public void eliminarActor(Actor actor) {
    }

    @Override
    public List<Actor> buscarActoresPorCodEscena(Integer codEscena) {
        List<Actor> actores = new ArrayList<Actor>();
        try {
            String sql = "{Call BuscarActoresPorCodEscena(?)}";
            con = getConnection();
            cs = con.prepareCall(sql);
            cs.setInt(1, codEscena);
            rs = cs.executeQuery();
            while (rs.next()) {
                actores.add(llenar(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ActorDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
        return actores;
    }

    @Override
    public List<Actor> listaActores() {
        List<Actor> actores = new ArrayList<Actor>();
        try {
            String sql = "{Call ListarActores()}";
            con = getConnection();
            cs = con.prepareCall(sql);
            rs = cs.executeQuery();
            while (rs.next()) {
                actores.add(llenar(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ActorDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            ConnectionFactory.closing(rs, cs, con);
        }
        return actores;
    }

    private Actor llenar(ResultSet rs) throws SQLException {
        Actor actor = new Actor();
        actor.setCodActor(rs.getInt("codActor"));
        actor.setNomArtistico(rs.getString("nomArtistico"));
        return actor;
    }

}
